package com.google.wavesurferrobot.textmining;

/*
*************************************************************************************************
* File:         IDFSpace.java                                                                   *
* Usage:        IDFSpace holds the IDF space of a collection of documents                       *
*************************************************************************************************
* IDE/Compiler: For the development needs we used NetBeans 5.0 and JRE 1.5.0_06 on WinXP Home   *
*************************************************************************************************
* License:      (LGPL) GNU Lesser General Public License                                        *
*               http://www.opensource.org/licenses/lgpl-license.php                             *
*************************************************************************************************
*         Originally written by George Tsatsaros (gbt@aueb.gr)                                  *      
*               Author         :  Panayiotis Papadopoulos (3010010)                             *
*               Website        :  http://dias.aueb.gr/~p3010010/                                *
*               E-mail         :  papado@freemail.gr                                            *
*                                 p3010010@dias.aueb.gr                                         *
*               MSN messenger  :  pap5@hotmail.com                                              *
*               Skype          :  papOnSlayer                                                   *
*                                                                                               *
* Contact:  Feel free to contact with me for any question/suggestion using the email(s) above   *
*************************************************************************************************
*/

import java.util.Enumeration;
import java.util.Hashtable;
import java.lang.Math.*;


public class IDFSpace
{
    //lexicon [token] - [number of documents that occurs]
    private Hashtable IDF;
    private Hashtable normIDF;


    public IDFSpace()
    {
        IDF = new Hashtable();
        normIDF = new Hashtable();
    }

    public boolean tokenExists(String aToken)
    {
        return IDF.containsKey(aToken);
    }


    private void addTokenToIDF(String aToken)
    {
        if (tokenExists(aToken))
        {
            Integer anInteger = (Integer) IDF.get(aToken);
            IDF.put(aToken, new Integer(anInteger.intValue() + 1));
        }
        else
            IDF.put(new String(aToken), new Integer(1));
    }


    public void updateIDF(Hashtable hash)
    {
        for (Enumeration e = hash.keys(); e.hasMoreElements(); )
            addTokenToIDF((String) e.nextElement());
    }

    public Hashtable getIDF()
    {
        return IDF;
    }

    public Integer getIDFFor(String aToken)
    {
        return (Integer) IDF.get(aToken);
    }


    public void computeNormIDF(int num)
    {
        String tmpString = new String("");
        Integer tmpInteger;
        double tmpDouble, tmp;
        Enumeration en = IDF.keys();
        while (en.hasMoreElements())
        {
            tmpString = (String) en.nextElement();
            tmpInteger = (Integer) IDF.get(tmpString);
            tmp = (double) (num + 1) / (double) tmpInteger.intValue();
            tmpDouble = (double) lg(tmp);
            normIDF.put(new String(tmpString), new Double(tmpDouble));
        }
    }


    public Hashtable getNormIDF()
    {
       return normIDF;
    }

    public Double getNormIDFFor(String aToken)
    {
        return (Double) normIDF.get(aToken);
    }

    public static double lg(double x)
    {
        //java.lang.Math log(double) returns the natural logarithm
        if (x==0)
            return 0;
        else
            return (Math.log(x) / Math.log(2));
    }

}//Class IDFSpace.
